//
//  MessageViewController.m
//  TestSchool
//
//  Created by 吴 晓泉 on 12-11-8.
//  Copyright (c) 2012年 yaoxia. All rights reserved.
//

#import "MessageViewController.h"
#import "UserManager.h"
#import "MessageDetailViewController.h"
#import "UserManager.h"
#import "MessageSendViewController.h"

@interface MessageViewController ()

@end

@implementation MessageViewController

@synthesize messageView,messageArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.messageArray = [NSMutableArray arrayWithCapacity:10];
    self.title = @"Messages";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Send" style:UIBarButtonItemStylePlain target:self action:@selector(goSend:)];
    
}

-(IBAction)goSend:(id)sender
{
    MessageSendViewController *detailController = [[MessageSendViewController alloc]initWithNibName:@"MessageSendViewController" bundle:nil];
    [self.navigationController pushViewController:detailController animated:YES];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self getNetData];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    
    self.messageView = nil;
    self.messageArray = nil;
    request = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

//------------表格数据--------------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.messageArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CommonTableIdentifier = @"CommonTableIdentifier";
    UITableViewCell *cell = nil;
    NSInteger row;
    
    cell = [tableView dequeueReusableCellWithIdentifier:CommonTableIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                                     reuseIdentifier:CommonTableIdentifier];
    }
    row = [indexPath row];
    //NSLog(@"commArray at %d:%@",row,[commonArray objectAtIndex:row]);
    if ([[UserManager getCurrentUser].name isEqualToString:[[self.messageArray objectAtIndex:row]objectForKey:@"to"]]) {
        cell.textLabel.text = [NSString stringWithFormat:@"Teacher:%@",[[self.messageArray objectAtIndex:row]objectForKey:@"content"]];
    }
    else
    {
        cell.textLabel.text = [NSString stringWithFormat:@"Me:%@",[[self.messageArray objectAtIndex:row]objectForKey:@"content"]];
    }
    
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageDetailViewController *detailController = [[MessageDetailViewController alloc]initWithNibName:@"MessageDetailViewController" bundle:nil];
    detailController.message = [self.messageArray objectAtIndex:[indexPath row]];
    [self.navigationController pushViewController:detailController animated:YES];
}

-(void)getNetData
{
    [self showWaitView:nil];
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    
    //NSString * URL_LOGIN = @"http://218.24.94.212/Test/login.jsp";
    NSString * URL_LOGIN = [NSString stringWithFormat:@"%@/Test/getAllMessage.jsp",delegate.baseUrl];
    
    NSURL *url = [NSURL URLWithString:URL_LOGIN];
    request = [ASIFormDataRequest requestWithURL:url];
    
    [request setPostValue:[UserManager getCurrentUser].schoolId forKey:@"fromschoolid"];
    [request setPostValue:@"000" forKey:@"toschoolid"];
    
    [request setCompletionBlock:^{
        
        [self hideWaitView];
        
        // Use when fetching text data
        NSString *responseString = [request responseString];
        
       // NSLog(@"responseString = %@",responseString);//显示收到的信息
        
        id json = [responseString JSONValue];
        //NSLog(@"meg = %@",[json objectForKey:@"drugInfo"]);
        if ([[json objectForKey:@"code"]intValue] == 500) {
            UIAlertView * messageAlert = [[UIAlertView alloc]
                                          initWithTitle:@"Message"
                                          message:[NSString stringWithFormat:@"%@",[json objectForKey:@"message"]]
                                          delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
            [messageAlert show];
            return ;
        }
        
        self.messageArray = [json objectForKey:@"messageArray"];
        [self.messageView reloadData];
        // Use when fetching binary data
        //NSData *responseData = [request responseData];
    }];
    [request setFailedBlock:^{
        [self hideWaitView];
        NSError *error = [request error];
        
        NSLog(@"error = %@",error);
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"Network error"
                              message:@"Please check your network"
                              delegate:self
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
        [alert show];
    }];
    [request startAsynchronous];
}


@end
